/**
 * @Title: LoginController.java
 * @Package com.leesche.zcsp.node.controller
 * @Description:()
 * @author dennies yang
 * @date 2016年9月21日 下午2:58:12
 * @version V1.0
 */
package com.leesche.zcsp.node.web.controller.web.computer;

import com.leesche.zcsp.node.utils.web.SessionManager;
import com.leesche.zcsp.node.utils.web.gernerator.CaptchaGernerator;
import com.leesche.zcsp.node.utils.web.messge.WebResult;
import com.leesche.zcsp.node.web.constants.Constants;
import com.leesche.zcsp.node.web.controller.template.LoginTemplate;
import com.leesche.zcsp.node.web.vo.Account;
import com.leesche.zcsp.node.web.vo.v2.Menu;
import com.leesche.zcsp.node.web.vo.Principal;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author dennies yang
 * @version V1.0
 * @Title: LoginController.java
 * @Package com.leesche.zcsp.node.controller
 * @Description:(登录界面)
 * @date 2016年9月21日 下午3:02:45
 */
@Controller
@RequestMapping()
public class LoginController extends LoginTemplate {

    private final Logger logger = Logger.getLogger(LoginController.class);

    private static final String LOGIN_VIEW = "web/login";

    @RequestMapping(value = {"", "/", "index", "home"})
    public String index(Model model) {
        Principal pc = privilegeService.getCurrentPrincipal();
        if (pc == null) {
            return "redirect:/login";
        } else {
            model.addAttribute("userName", pc.getUsername());
            return "web/main";
        }
    }

    @RequestMapping(value = {"/login"})
    public String showLoginView() {
        //return "web/login";
        return LOGIN_VIEW;
    }

    /**
     * @param accout 登录的账户
     * @Title: login(Account accout)
     * @Description: TODO(PC端用户登录入口)
     * @author Jerry
     * @date 2016-10-15 12:44:49
     */
    @ResponseBody
    @RequestMapping(value = "/signin", method = RequestMethod.POST)
    public Map<String, Object> login(Account accout) {
        try {

            if (!SessionManager.getSessionAttribute(Constants.LOGIN_CODE).equals(accout.getVildCode())) {
                return WebResult.failure("验证码输入错误").toMap();
            }

            return super.userLogin(accout, Constants.MODULE_TYPE_WEB);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            return WebResult.failure(Constants.SYSTEM_ERROR_PROMPT_MSG).toMap();
        }
    }

    /**
     * @Title: quit()
     * @Description: TODO(PC端用户登出入口)
     * @author Jerry
     * @date 2016-10-15 12:44:49
     */
    @ResponseBody
    @RequestMapping(value = "/signout", method = RequestMethod.DELETE)
    public Map<String, Object> quit() {
        try {
            if (privilegeService.getCurrentPrincipal() != null) {
                return super.userQuit();
            } else {
                return WebResult.failure("用户未登录，请先登录").toMap();
            }
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            return WebResult.failure(Constants.SYSTEM_ERROR_PROMPT_MSG).toMap();
        }
    }


    /**
     * @return 侧边栏目
     * @Title: menu()
     * @Description: TODO(获取用户的可读权限)
     * @author Jerry
     * @date 2016-10-15 12:46:17
     */
    @RequestMapping(value = "/menu", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public List<Menu> menu() {
        return sysModuleService.findSysModuleTrees();
    }

    /**
     * @Title: getAuthCode @Description: TODO(验证码) @param @param request @param @param
     * response @param @param session @param @throws IOException @return
     * void @throws
     */
    @RequestMapping({"authCode"})
    public void getAuthCode(HttpServletRequest request,
                            HttpServletResponse response, HttpSession session)
            throws IOException {
        // 设置response头信息
        // 禁止缓存
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        // 将字符保存到session中用于前端的验证
        session.setAttribute(Constants.LOGIN_CODE,
                CaptchaGernerator.ImageGernerator(response.getOutputStream()));

    }

}
